

#include <iostream>
#include <map>


using namespace std;

int main() {
    
    int x,y,i,j,k,count=0;
    string keyword,message,new_message,encrypted_text,decrypted_text,row_coordinates,column_coordinates,location_of_x;
    
    cout<<"\nEnter the size of matrix \n";
    cin>>x>>y;
    
    char matrix[x][y];
    map<char,int> flag;
    
    memset(matrix, '0', sizeof(matrix[0][0]) * x * y);

    cout<<"Enter the keyword\n";
    cin>>keyword;
    
    cout<<"Enter the message to be encrypted: \n";
    cin>>message;
    
    for(i=0;message[i]!='\0';i++)
    {
        if((message[i]==message[i-1])&&count%2!=0)
            
        {
            
            new_message+='x';
            location_of_x+=i+'0';
            count++;
        }
        new_message+=message[i];
        count++;
    }
    if(count%2==1)
    {
        new_message+='x';

        location_of_x+=i+'0';
        
       

    }
    
    for(i=0,k=0;keyword[i]!='\0';i++,k++)
    {

        
            matrix[0][k]=keyword[i];
            flag[keyword[i]]=1;
        for(j=0;new_message[j]!='\0';j++)
            if(keyword[i]==new_message[j])
            {
                
                row_coordinates+=new_message[j];
                column_coordinates+=new_message[j];
                row_coordinates+=(k/5)+'0';
                column_coordinates+=(k%5)+'0';
            }

    }
    
    
    
    

            for(i=97;i<=122;i++)
            {
            
                if(flag[(char)i]==0)
                {
                matrix[0][k]=(char)i;
                
                    for(j=0;new_message[j]!='\0';j++)
                    if(matrix[0][k]==new_message[j])
                    {

                    row_coordinates+=new_message[j];
                    column_coordinates+=message[j];
                    row_coordinates+=(k/5)+'0';
                    column_coordinates+=(k%5)+'0';
                    }
                k++;
                
                }
            }
    
    
   

    
    
    k=0;
label:for(i=0;i<x&&new_message[k]!='\0';i++)
      for(j=0;j<y;j++)
        if(matrix[i][j]==new_message[k])
        {
            
            
            
            if(k%2==0)
            {
                int row_value=(int)row_coordinates[row_coordinates.find(new_message[k+1])+1]-48;
                int column_value=(int)column_coordinates[column_coordinates.find(new_message[k+1])+1]-48;

                if(row_value==i)
                {
                    int column_value=j+1;
                    if(column_value>=y)
                        column_value=0;
                    
                    encrypted_text+=matrix[row_value][column_value];
                }
                else if(column_value==j)
                {
                
                    int row_value=i+1;
                    if(row_value>=x)
                        row_value=0;
                    encrypted_text+=matrix[row_value][column_value];


                }
                
                else
            encrypted_text+=matrix[row_value][j];
              
            }
            
            if(k%2==1)
            {
                int row_value=(int)row_coordinates[row_coordinates.find(new_message[k-1])+1]-48;
                int column_value=(int)column_coordinates[column_coordinates.find(new_message[k-1])+1]-48;
                
                if(row_value==i)
                {
                    int column_value=j+1;
                    if(column_value>=y)
                        column_value=0;
                    
                    encrypted_text+=matrix[row_value][column_value];
                }
                else if(column_value==j)
                {
                    
                    int row_value=i+1;
                    if(row_value>=x)
                        row_value=0;
                    encrypted_text+=matrix[row_value][column_value];
                    
                    
                }
                
                else

                
                encrypted_text+=matrix[row_value][j];
              
            }
                k++;
            goto label;
        }
    
    

    cout<<"Encrypted text is: "<<encrypted_text<<endl;
    
    new_message=row_coordinates=column_coordinates="";
    

    new_message=encrypted_text;
   
    for(i=0,k=0;keyword[i]!='\0';i++,k++)
    {
        
        
        matrix[0][k]=keyword[i];
        flag[keyword[i]]=1;
        for(j=0;new_message[j]!='\0';j++)
            if(keyword[i]==new_message[j])
            {
                
                row_coordinates+=new_message[j];
                column_coordinates+=new_message[j];
                row_coordinates+=(k/5)+'0';
                column_coordinates+=(k%5)+'0';
            }
        
    }
    
    
    
    
    for(i=97;i<=122;i++)
    {
        
        if(flag[(char)i]==0)
        {
            matrix[0][k]=(char)i;
            
            for(j=0;new_message[j]!='\0';j++)
                if(matrix[0][k]==new_message[j])
                {
                    
                    row_coordinates+=new_message[j];
                    column_coordinates+=new_message[j];
                    row_coordinates+=(k/5)+'0';
                    column_coordinates+=(k%5)+'0';
                }
            k++;
            
        }
    }
    
    k=count=0;
   
    

label2:for(i=0;i<x&&encrypted_text[k]!='\0';i++)
{
    if(k==(location_of_x[count]-48))
    {
        count++;
        k++;
        continue;
    }
    
    for(j=0;j<y;j++)
        if(matrix[i][j]==encrypted_text[k])
        {
            
            
            if(k%2==0)
            {
                int row_value=(int)row_coordinates[row_coordinates.find(encrypted_text[k+1])+1]-48;
                int column_value=(int)column_coordinates[column_coordinates.find(encrypted_text[k+1])+1]-48;
                
                if(row_value==i)
                {
                    cout<<"111111"<<endl;
                    int column_value=j-1;
                    if(column_value<0)
                        column_value=y-1;
                    
                    decrypted_text+=matrix[row_value][column_value];
                }
                else if(column_value==j)
                {
                    
                    int row_value=i-1;
                    if(row_value<0)
                        row_value=x-1;
                    
                  
                    decrypted_text+=matrix[row_value][column_value];
                    
                    
                }
                
                else
                    decrypted_text+=matrix[row_value][j];
                
            }
            
            if(k%2==1)
            {
                int row_value=(int)row_coordinates[row_coordinates.find(encrypted_text[k-1])+1]-48;
                int column_value=(int)column_coordinates[column_coordinates.find(encrypted_text[k-1])+1]-48;
                
                if(row_value==i)
                {

                    int column_value=j-1;
                    if(column_value<0)
                        column_value=y-1;
                    
                    decrypted_text+=matrix[row_value][column_value];
                }
                else if(column_value==j)
                {

                    int row_value=i-1;
                    if(row_value<0)
                        row_value=x-1;
                    decrypted_text+=matrix[row_value][column_value];
                    
                    
                }

                
                else
                    
                    
                    decrypted_text+=matrix[row_value][j];
                
            }
            k++;
            goto label2;
        }
    
}
    
    
    
    cout<<"Decrypted text is: "<<decrypted_text<<endl;

    
        
        return 0;
}


/*
OUTPUT:-

 
 Enter the size of matrix
 5 5
 Enter the keyword
 monarchy
 Enter the message to be encrypted:
 vitpune
 Encrypted text is: gwkvovtj
 Decrypted text is: vitpune

 
 Enter the size of matrix
 5 5
 Enter the keyword
 monarchy
 Enter the message to be encrypted:
 balloon
 Encrypted text is: ibusuhna
 Decrypted text is: balloon
 
 */